import CryptoJS from "crypto-js"
import JSEncrypt from "jsencrypt"

export function encryptPassword(password: string, publicKey: string): string {
	// Encrypt with the public key...
	const encrypt = new JSEncrypt();
	encrypt.setPublicKey(publicKey);
	const encrypted = encrypt.encrypt(password);
	if (!encrypt) {
		throw Error("Encryption failed!")
	}
	return encrypted;
}

export function storeToken(token: string): void {
	uni.setStorage({
		key: 'TOKEN',
		data: token,
	});
}

export async function getToken(): Promise<string | null> {
	return new Promise((resolve, reject) => {
		uni.getStorage({
			key: "TOKEN",
			success(val: { data: string }) {
				console.log(val)
				resolve(val.data);
			},
			fail(err) {
				reject(err)
			}
		})
	})
}